<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>收银界面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
    <link rel="canonical" href="https://serratus.github.io/v1.0.0-beta.1/examples/scan-to-input/" />
    <link rel="stylesheet" href="../../../css/styles.css">
    <link rel="stylesheet" href="../../../css/example.css">
    <link rel="stylesheet" href="../../../css/pygment_trac.css">
    <link rel="stylesheet" href="../../../css/title.css">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <script>
        var host = "serratus.github.io";
        if ((host == window.location.host) && (window.location.protocol != "https:")) {
            window.location.protocol = "https";
        }
    </script>
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

      ga('create', 'UA-56318310-1', 'auto');
      ga('send', 'pageview');

    </script>
    <script src="../../../js/jquery-1.9.0.min.js"></script>
    <script src="../../../js/quagga.js"></script>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
		<!--表格开始-->

        <table class="layui-hide" id="salesTable" lay-filter="salesTable"></table>

		<script type="text/html" id="salesTableBar">
            <a class="layui-btn layui-btn-sm layui-btn-warm" lay-event="update">数量</a>
		    <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</a>
		</script>




        <!-- 选择菜单和修改的弹出层开始-->
        <div id="selectMenuDiv" style="display: none;padding: 5px;">

            <table class="layui-hide" id="menuTable" lay-filter="menuTable"></table>
        </div>


        <!-- 结账 -->
        <div>
            <!-- 扫码 -->
            <!-- input-field -->
            <div class="layui-inline layui-col-md8">
                <!-- style="display: none;" -->
                <!-- oninput="myFunction()" -->
                <input id="isbn_input" lay-verify="number" class="isbn" style="display: none;"/>
                <button type="button" class="layui-btn icon-barcode button scan" style="background-color: white;"></button>
                <input placeholder="请输入商品条形码" name="id" class=" layui-input-inline" id="inputId" style="width: 190px; height:36px;">
                <button class="layui-btn layui-btn-warm " lay-event="add" id="finalcommit">确认</button>
                <button class="layui-btn" lay-event="add" id="inputMember">输入会员</button>
            </div>
            <!-- layui-bg-number -->
            <div class="panel layui-inline layui-col-md2">
                <div class="panel-body" >
                    <div class="panel-content ">
                        <small>数量</small>
                        <span class="no-margins layui-inline" id="cargo_num" style="font-size: 16px;">0</span>
                        
                    </div>
                    <div class="panel-content">
                        <!-- <h1 class="no-margins">1234</h1> -->
                        <small>总金额</small>
                        <span class="no-margins layui-inline" id="totalPrice" style="font-size: 20px; color: #FF5722;">0</span>
                    </div>
                </div>
            </div>
            <div class="layui-inline">
                <button class="layui-btn layui-btn-danger" id="endToHome">结账</button>
            </div>
        </div>
        <!-- 会员信息显示 -->
        <div>
            
            <table class="layui-table" lay-filter="list_member" id="list_member" style="width: 100%;">
            </table>
        </div>

        <div style="display: none;" id="searchTableBar">
            <a class="layui-btn layui-btn-sm layui-btn-danger data-count-delete btn_up layui-bg-blue" lay-event="search">查询商品</a>
        </div>

        <!-- 添加会员弹出层 -->
        <div id ="addOrUpdateDiv" style="display: none;padding: 5px;">
            <form method="post" class="layui-form layui-form-pane" lay-filter="dataFrm" id="dataFrm">
                <div class="layui-form-item">
                    <label class="layui-form-label">会员手机号</label>
                    <div class="layui-input-block">
                        <input placeholder="请输入会员手机号" lay-verify="required|number" name="phone" id="phone" class="layui-input"></input>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-block" style="text-align: center;">
                        <a class="layui-btn " lay-submit=""  lay-filter="doSubmit2" id="queren"><label class="layui-icon layui-icon-release"></label>确认</a>
                        <a class="layui-btn layui-btn-warm" onclick="javascript:$('#dataFrm')[0].reset()" lay-filter="data-search-btn"><label class="layui-icon layui-icon-refresh"></label>重置</a>
                    </div>
                </div>
            </form>
        </div> 



        <!-- 结账收银的弹出层 -->
        <div id="endSalesDiv" style="display: none;" >
            <form method="post" class="layui-form layui-form-pane" lay-filter="endDataFrm" id="endDataFrm">
                <div class="layui-form-item">
                    <label class="layui-form-label">消费金额</label>
                    <div class="layui-input-block">
                        <input lay-verify="required|number" name="money" id="money" class="layui-input" disabled="disabled"></input>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">实收金额</label>
                    <div class="layui-input-block">
                        <input lay-verify="required|number" name="endMoney" id="endMoney" class="layui-input" ></input>
                    </div>
                </div>
                <div class="layui-form-item">
                        <label class="layui-form-label">支付类型</label>
                        <div class="layui-input-inline">
                            <input type="radio" lay-filter="pay" name="pay_type" value="现金" title="现金" checked="checked" class="layui-input">
                            <input type="radio" lay-filter="pay" name="pay_type" value="微信" title="微信" class="layui-input">
                            <input type="radio" lay-filter="pay" name="pay_type" value="支付宝" title="支付宝" class="layui-input">
                        </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-block" style="text-align: center;">
                        <a class="layui-btn " lay-submit=""  lay-filter="doSubmit3" id="endSales"><label class="layui-icon layui-icon-release"></label>确认</a>
                        <!-- <a class="layui-btn layui-btn-warm" onclick="javascript:$('#endDataFrm')[0].reset();" lay-filter="data-search-btn"><label class="layui-icon layui-icon-refresh"></label>取消</a> -->
                    </div>
                </div>
            </form>
        </div>

        <!-- 修改数量的弹出层 -->
        <div id="updateSalesDiv" style="display: none;" >
            <form method="post" class="layui-form layui-form-pane" lay-filter="updateDataFrm" id="updateDataFrm">
                <div class="layui-form-item">
                    <label class="layui-form-label">数量</label>
                    <div class="layui-input-block">
                        <input placeholder="请输入最终的数量" lay-verify="required|number|nonenil" name="goods_num" id="goods_num" class="layui-input"></input>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-block" style="text-align: center;">
                        <a class="layui-btn " lay-submit=""  lay-filter="doSubmit4" id="updateSales"><label class="layui-icon layui-icon-release"></label>确认</a>
                    </div>
                </div>
            </form>
        </div>

        <!-- 查询商品的弹出层 -->
        <div id="searchGoodsDiv" style="display: none;" >
            <form method="post" class="layui-form layui-form-pane" lay-filter="searchDataFrm" id="searchDataFrm">
                <div class="layui-form-item">
                    <label class="layui-form-label">商品ID</label>
                    <div class="layui-input-block">
                        <input placeholder="请输入商品的ID" lay-verify="required|number|nonenil" name="goodsid" class="layui-input"></input>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-block" style="text-align: center;">
                        <a class="layui-btn " lay-submit=""  lay-filter="doSubmit5" id="searchGoods"><label class="layui-icon layui-icon-release"></label>确认</a>
                    </div>
                </div>
            </form>
        </div>

        <!-- 查询的弹窗 -->
        <div id ="goodsDiv" style="display: none;padding: 5px;">
            <form method="post" class="layui-form layui-form-pane" lay-filter="dataFrm1" id="dataFrm1">
                
                <div class="layui-col-md12 layui-col-xs12">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md9 layui-col-xs7">
            
                         <div class="layui-form-item magt3">
                             <label class="layui-form-label">商品名称</label>
                             <div class="layui-input-block">
                                  <input type="hidden" name="id" />
                                 <input type="text" name="goodsname" disabled="disabled" class="layui-input" lay-verify="required" placeholder="请输入商品名称">
                             </div>
                         </div>
                         <div class="layui-form-item magt3">
                             <label class="layui-form-label">商品描述</label>
                             <div class="layui-input-block">
                                 <input type="text" name="description" disabled="disabled"  class="layui-input" lay-verify="required" placeholder="请输入商品描述">
                             </div>
                         </div>
                        </div>
                        <div class="layui-col-md3 layui-col-xs5">
                            <div class="layui-upload-list thumbBox mag0 magt3">
                             <input type="hidden" name="goodsimg" id="goodsimg" />
                                <img class="layui-upload-img thumbImg">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item magb0">
                        <div class="layui-inline">
                         <label class="layui-form-label">产地</label>
                         <div class="layui-input-inline">
                             <input type="text" name="produceplace" disabled="disabled" class="layui-input " lay-verify="required" placeholder="请输入商品产地">
                         </div>
                     </div>
                     <div class="layui-inline">
                         <label class="layui-form-label">包装</label>
                         <div class="layui-input-inline">
                             <input type="text" name="goodspackage" disabled="disabled" class="layui-input " lay-verify="required" placeholder="请输入商品包装">
                         </div>
                     </div>
                     <div class="layui-inline">
                         <label class="layui-form-label">规格</label>
                         <div class="layui-input-inline">
                             <input type="text" name="size" disabled="disabled" class="layui-input " lay-verify="required" placeholder="请输入商品规格">
                         </div>
                     </div>
                     <div class="layui-inline">
                         <label class="layui-form-label">生产批号</label>
                         <div class="layui-input-inline"> 
                             <input type="text" name="productcode" disabled="disabled" class="layui-input " lay-verify="required" placeholder="请输入商品生产批号">
                         </div>
                     </div>
                     <div class="layui-inline">
                         <label class="layui-form-label">批准文号</label>
                         <div class="layui-input-inline">
                             <input type="text" name="promitcode" disabled="disabled" class="layui-input " lay-verify="required" placeholder="请输入商品批准文号">
                         </div>
                     </div>
                     <div class="layui-inline">
                         <label class="layui-form-label">销售价格</label>
                         <div class="layui-input-inline">
                             <input type="text" name="price" disabled="disabled" id="price0" class="layui-input " lay-verify="required" placeholder="请输入商品价格">
                         </div>
                     </div>
                     <div class="layui-inline">
                        <label class="layui-form-label">会员价格</label>
                        <div class="layui-input-inline">
                            <input type="text" name="memberprice" disabled="disabled" id="memberprice0" class="layui-input " lay-verify="required" placeholder="请输入商品库存量">
                        </div>
                    </div>
                     <div class="layui-inline">
                         <label class="layui-form-label">库存量</label>
                         <div class="layui-input-inline">
                             <input type="text" name="number" id="number1" disabled="disabled" class="layui-input " lay-verify="required|number" placeholder="请输入商品库存量">
                         </div>
                     </div>
                     <div class="layui-inline">
                        <label class="layui-form-label">生产时间</label>
                        <div class="layui-input-inline">
                            <input type="text" name="productiondate" class="layui-input " disabled="disabled" lay-verify="required" placeholder="请输入商品库存预警值">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">到期时间</label>
                        <div class="layui-input-inline">
                            <input type="text" name="expiration" class="layui-input " disabled="disabled" lay-verify="required" placeholder="请输入商品库存预警值">
                        </div>
                    </div>
                    </div>
                </div>

             <div class="layui-form-item">
                 <div class="layui-input-block" style="text-align: left;">
                     <a class="layui-btn " lay-submit=""  lay-filter="doSubmit">关闭</a>
                 </div>
              </div>
            </form>
        </div>

        <!-- 打印小票的弹窗-->
        <div id="ticket" style="display: none;padding: 5px;background-color: #fff;">
            <div class="print_container">
                <h1>美宜佳-更亲近的伙伴</h1>
                <span>****************************************</span>
                <div class="section1">
                    <h3>交易小票</h3>
                    <h3>广东财经大学(战时补给店)</h3>
                </div>
                <span>****************************************</span>
                <div class="section2">
                    <label id="projectid_ticket">订单编号：1110</label>
                    <label id="paytype_ticket">交易方式：现金</label>
                </div>
                <span>****************************************</span>
                <div class="section3">
                    <label id="opreator_ticket">操作人：西巴</label>
                    <label id="date_ticket">下单时间：2021/12/16 CST</label>
                </div>
                <span>****************************************</span>
                <div class="section4">
                    <div style="border-bottom: 1px solid #DADADA;">
                        <table id="table_ticket" style="width: 100%;">
                            <thead>
                                <tr>
                                    <td width="60%">商品名称</td>
                                    <td width="20%">数量</td>
                                    <td width="20%">金额</td>
                                    <td width="20%">会员价</td>
                                </tr>
                            </thead>
                        </table>
                    </div>
                    <div class="other_fee">
                        <div class="canh">
                            <label class="left">总数量</label>
                            <label id="number_ticket" class="right">0</label>
                            <div class="clearfix"></div>
                        </div>
                        <div class="peis">
                            <label class="left">需收金额</label>
                            <label id="needprice_ticket" class="right">0</label>
                            <div class="clearfix"></div>
                        </div>
                        <div class="manj">
                            <label class="left">实收金额</label>
                            <label id="inprice_ticket" class="right">0</label>
                            <div class="clearfix"></div>
                        </div>
                        <div class="manj">
                            <label class="left">找零</label>
                            <label id="charge_ticket" class="right">0</label>
                            <div class="clearfix"></div>
                        </div>
                    </div>
                    <div class="total">
                        <label class="left">总计</label>
                        <label id="total_ticket" class="right">39</label>
                        <div class="clearfix"></div>
                    </div>
                    <div style="text-align: right;">
                        <label>顾客已付款</label>
                    </div>
                    <span>****************************************</span>
                </div>
                <div class="section5">
                    <label>门店编码：117A</label>
                    <label>服务热线：400-887-1133</label>
                    <label>此单为售后服务的唯一凭证</label>
                </div>    
                <span>****************************************</span>
                <div>
                    <img src="../../../img/QRscan.png" alt="">
                </div>
            </div>
        </div>
        <!--打印小票结束-->
    </div>
</div>
<script src="../../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../../lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="../../../lib/common/jquery.cookie.min.js" charset="utf-8"></script>
<script src="../../../lib/common/common.js" charset="utf-8"></script>


<script>
    var index_member, scan_id, update_id,endSales_id,search_id,mainIndex, memberState = 0 , allNum = 0, allPrice = 0, payType = "现金", selectItem;
    var tabledata, tableinner, frominner, change , inprice, needprice;
    inputMember.onclick=function(){
        index_member=layer.open({
            type:1,
            title:'输入会员',
            content:$("#addOrUpdateDiv"),
        });
    }


    //扫描组件
    var Quagga = window.Quagga;
    var App = {
        _scanner: null,
        init: function() {
            this.attachListeners();
        },
        activateScanner: function() {
            var scanner = this.configureScanner('.overlay__content'),
                onDetected = function (result) {
                    document.querySelector('input.isbn').value = result.codeResult.code;
                    myFunction(result.codeResult.code);
                    // console.log(typeof(result.codeResult.code))
                    // selectItemFromGoods(Number(result.codeResult.code));
                    stop();
                }.bind(this),
                stop = function() {
                    scanner.stop();  // should also clear all event-listeners?
                    scanner.removeEventListener('detected', onDetected);
                    this.hideOverlay();
                    this.attachListeners();
                }.bind(this);

            this.showOverlay(stop);
            scanner.addEventListener('detected', onDetected).start();
        },
        attachListeners: function() {
            var self = this,
            // .input-field
                button = document.querySelector('input + button.scan');
            button.addEventListener("click", function onClick(e) {
                e.preventDefault();
                button.removeEventListener("click", onClick);
                self.activateScanner();
            });
        },
        showOverlay: function(cancelCb) {
            if (!this._overlay) {
                var content = document.createElement('div'),
                    closeButton = document.createElement('div');

                closeButton.appendChild(document.createTextNode('X'));
                content.className = 'overlay__content';
                closeButton.className = 'overlay__close';
                this._overlay = document.createElement('div');
                this._overlay.className = 'overlay';
                this._overlay.appendChild(content);
                content.appendChild(closeButton);
                closeButton.addEventListener('click', function closeClick() {
                    closeButton.removeEventListener('click', closeClick);
                    cancelCb();
                });
                document.body.appendChild(this._overlay);
            } else {
                var closeButton = document.querySelector('.overlay__close');
                closeButton.addEventListener('click', function closeClick() {
                    closeButton.removeEventListener('click', closeClick);
                    cancelCb();
                });
            }
            this._overlay.style.display = "block";
        },
        hideOverlay: function() {
            if (this._overlay) {
                this._overlay.style.display = "none";
            }
        },
        configureScanner: function(selector) {
            if (!this._scanner) {
                this._scanner = Quagga
                // ean_reader
                    .decoder({readers: ['code_128_reader']})
                    .locator({patchSize: 'medium'})
                    .fromSource({
                        target: selector,
                        constraints: {
                            width: 800,
                            height: 600,
                            facingMode: "environment"
                        }
                    });
            }
            return this._scanner;
        }
    };
    App.init();

    
    function myFunction(params3){
     
        selectItemFromGoods1(params3);
    }
    // 扫码
        function selectItemFromGoods1(params){
            // {id:params}
            $.post(api+'goods/getGoodsByGoodIdSales',{id:params},function(res){
                var flag = 0;
                if(res.code == -1){
                    layer.msg(res.msg);
                }else{
                    //只获取一次
                    if(tabledata == null || tabledata == ""){
                        tabledata = tableinner.cache["salesTable"];  //获取现有数据
                    }
                    //添加后台传过来的数据(全部处理)
                    if(tabledata.length == 0){
                        tabledata.push(res.obj1);
                    }else{
                        tabledata.forEach(element => {
                            if(element.id == res.obj1.id){
                                element.number ++ ;
                                flag = 1 ;
                            }// 存在该id
                        });
                        //不存在的情况
                        if (!flag){
                            tabledata.push(res.obj1);
                            flag = 0 ;
                        }
                    }
                    //同步信息
                    tabledata = checkPrice_Num(memberState, tabledata);
                }
            })
        };
        //添加表单选项单品操作(已完成合并内容)
        function selectItemFromGoods(params){
            // {id:params}
            $.post(api+'goods/getGoodsByGoodIdSales',params,function(res){
                var flag = 0;
                if(res.code == -1){
                    layer.msg(res.msg);
                }else{
                    //只获取一次
                    if(tabledata == null || tabledata == ""){
                        tabledata = tableinner.cache["salesTable"];  //获取现有数据
                    }
                    //添加后台传过来的数据(全部处理)
                    if(tabledata.length == 0){
                        tabledata.push(res.obj1);
                    }else{
                        tabledata.forEach(element => {
                            if(element.id == res.obj1.id){
                                element.number ++ ;
                                flag = 1 ;
                            }// 存在该id
                        });
                        //不存在的情况
                        if (!flag){
                            tabledata.push(res.obj1);
                            flag = 0 ;
                        }
                    }
                    //同步信息
                    tabledata = checkPrice_Num(memberState, tabledata);
                }
            })
        };


        //价格更新&总价$数量(根据传入数据集改变)
        function checkPrice_Num(states, dataset){
            //null
            allPrice = 0;
            allNum = 0;
            $("#cargo_num").text(allNum);
            $("#totalPrice").text(allPrice);
            if(dataset == null || dataset == ""){
                return null;
            }

            dataset.forEach(element => {
                //会员
                if (states){
                    element.total = element.number * element.memberprice;
                    allPrice += element.total;
                }else{
                    element.total = element.number * element.price;
                    allPrice += element.total;
                }
                allNum += element.number;
            });

            $("#cargo_num").text(allNum);
            $("#totalPrice").text(allPrice);

            tableinner.reload("salesTable",{
                url:"",             //真的司马，这个代码耗时2h
                page: {
                    curr: 1
                },
                data: dataset,
            })
            return dataset;
        };



    layui.use(['form', 'table','laydate','layer'], function () {
        var form = layui.form;
        var table = layui.table;
		var laydate=layui.laydate;
		var layer=layui.layer;
        tableinner = table;
        frominner = form;

      var tableIns=table.render({
            elem: '#salesTable',
            url: api+'sales/startSales',
			toolbar:"#searchTableBar",
            height:'full-220',
            text: {none: "请输入或扫描商品编号开始收银"},
			cellMinWidth:true,
            loading: true,
            cols: [[
                {field: 'id', title: '条形码', align: "center"},
                {field: 'goodsname',  title: '商品名称', align: "center"},                
                {field: 'number',  title: '数量', align: "center"},
                {field: 'price',  title: '单价', align: "center"},
                {field: 'memberprice',  title: '会员价', align: "center"},
                {field: 'total',  title: '小计', align: "center"},
                {title: '操作', templet: '#salesTableBar', fixed: "right", align: "center"}
            ]],
            page: true
        });

        table.on('toolbar(salesTable)', function(obj){
            var checkStatus1 = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'search':
                    openSearchDiv();
                    break;
            }
        })


        function openSearchDiv(){
            search_id = layer.open({
                type: 1,
                title: '查询商品信息',
                content: $("#searchGoodsDiv"),
                success: function(index){
                    $("#searchDataFrm")[0].reset();
                }
            })
        }

        function openGoodsDiv(data){
            mainIndex = layer.open({
                type: 1,
                title: '详细商品信息',
                content: $("#goodsDiv"),
                success: function(index){
                    $("#dataFrm1")[0].reset();
                    form.val("dataFrm1",data);
                }
            })
        }
        

        //查询提交
        form.on("submit(doSubmit5)",function(data){
            var params = $("#searchDataFrm").serialize();
            $.post(api+"goods/getGoodsByGoodId2",params,function(res){
                if(res.code == -1){
                    layer.msg(res.msg);
                }else{
                    openGoodsDiv(res.obj1);
                }
                layer.close(search_id);
            })
        });

        //监听查询完成的弹出层
        form.on("submit(doSubmit)",function(data){
            layer.close(mainIndex);
        });

        //提交会员查询数据
        form.on("submit(doSubmit2)",function(data){
            var params = $("#phone").serialize();
            $.post(api+'member/queryPhoneMember',params,function(res){
                
                if(res!=null && res!=""){
                    layer.msg("查询成功");
                    //关闭查询弹出层
                    layer.close(index_member);
                    memberState = 1 ;

                    //应当显示会员信息了
                    var member_tableIns = table.render({
                        elem: '#list_member',
                        url: "",
                        cellMinWidth:true,
                        data:[res],
                        cols:[[
                            {field: 'cardid', title: '会员ID', align: "center"},
                            {field: 'name', title: '会员名', align: "center"},
                            {field: 'registrationtime', title: '注册时间', align: "center"},
                            {field: 'phone', title: '手机号', align: "center"}
                        ]],
                        page: false
                    });

                    $("#phone").val("");
                    tabledata = checkPrice_Num(memberState, tabledata);
                }
                else{
                    layer.msg("查询失败，无此会员信息");
                    layer.close(index_member);
                    $("#phone").val("");
                    memberState = 0 ;
                }
            })

            
            return false;
        })

        //结账按钮
        endToHome.onclick = function(){
            // location.reload();
            var flag = 0 ;
            if(tabledata){
                //先关闭异步
                $.ajaxSettings.async = false;
                tabledata.forEach(element => {
                    $.get(api+"goods/getGoodsByGoodId",{goodsid:element.id},function(res){
                        if (res.data.number < element.number){
                            layer.msg(element.goodsname+"的库存不足，数量："+res.data.number);
                            flag = 1 ;
                        }
                    })
                });
                if(!flag){
                    endSales_id = layer.open({
                    type: 1,
                    title: '结账收银',
                    area:['350px','300px'],
                    content:$("#endSalesDiv"),
                    success: function(index){
                        $("#endDataFrm")[0].reset();
                        $("#money").val(allPrice);
                        //开启同步
                        $.ajaxSettings.async = true;
                    }
                })
                }

            }else{
                layer.msg("未检测扫描商品！");
            }
        }

        //最终提交数据
        form.on("submit(doSubmit3)",function(data){

            inprice = Number($("#endMoney").val());
            if(Number($("#money").val()) > Number($("#endMoney").val())){
                layer.msg("当前的支付金额不足以完成此次交易！");
            }else{
                //金额充足情况
                $.ajaxSettings.async = false;
                //请求最大的交易id
                $.get(api+"sales/queryMaxProjectById",function(res){
                    //数据处理
                    tabledata.forEach(element => {
                        //会员处理
                        if(memberState){
                            element.customerid = table.cache["list_member"][0].cardid;
                        }else{
                            element.customerid = 0;
                        }
                        //支付方式
                        element.paytype = payType;

                        //最大支付id赋值
                        element.projectid = res;

                        //处理完成后传回后端            
                        $.post(api+"sales/saveSaleInformation",element,function(res){
                            if(res.code != 200){
                                layer.msg("交易出错！请重新结账");
                                layer.close(endSales_id);
                            }
                        })
                    });
                    
                })
                $.ajaxSettings.async = true;

                layer.close(endSales_id);

                change = $("#endMoney").val()-$("#money").val();
                layer.confirm("需找零："+(change)+"(元)。是否打印小票",{
                    btn: ['打印小票']
                },function(index){
                    //小票打印的逻辑
                    openTicketDiv();


                    layer.close(index);
                    // init_data();
                    // location.reload();
                })    
            }
            return false;
        })

        //打印小票弹窗
        function openTicketDiv(){
            mainIndex = layer.open({
                type: 1,
                title:"",
                content:$("#ticket"),
                area: ['300px','700px'],
                btn:['完成'],
                success: function(index){
                    //上层
                    $("#projectid_ticket").text("订单编号："+tabledata[0].projectid);
                    $("#paytype_ticket").text("交易方式："+payType);
                    $.ajaxSettings.async = false;
                    $.get(api+"sales/salesOperator",function(res){
                        $("#opreator_ticket").text("操作人："+res.opertor);
                        $("#date_ticket").text("下单时间："+res.date);
                    })
                    $.ajaxSettings.async = true;

                    //下层
                    $("#number_ticket").text(allNum);
                    $("#needprice_ticket").text(allPrice);
                    $("#inprice_ticket").text(inprice);
                    $("#charge_ticket").text(change);
                    $("#total_ticket").text(allPrice);

                    //商品栏
                    var tab = $("#table_ticket").html();
                    var html_ticker = tab + "<tbody>";
                    tabledata.forEach(element => {
                        html_ticker+="<tr><td>"+element.goodsname+"</td><td>"+element.number+"</td><td>"
                            +element.price+"</td><td>"+element.memberprice+"</td></tr>";
                    });
                    html_ticker += "</tbody>";
                    $("#table_ticket").html(html_ticker);
                    
                },
                yes: function(index){
                    location.reload()
                }
            })
        }


        finalcommit.onclick=function(){
            //调用接口
            var params2 = $("#inputId").serialize();
            selectItemFromGoods(params2);
        }

        //单选按钮的监听
        form.on('radio(pay)', function(data){
            if(data.elem.checked){
                payType = data.value;
            }
            return false;
        })

        //初始化数据
        function init_data(){
            memberState = 0 ;
            tabledata = null ;
            allNum = 0 ;
            allPrice = 0;
            payType = "现金";
            selectItem = null;

        }


        //监听行数据的事件
        table.on('tool(salesTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'delete') {
                layer.confirm('是否确认删除此商品?', function (index) {
					obj.del();
                    for(let i =0 ; i < tabledata.length; i ++){
                        if(data.id == tabledata[i].id){
                            tabledata.splice(i,1);
                        }
                    }
                    tabledata = checkPrice_Num(memberState,tabledata);
                    layer.close(index);
                });
            }else if(obj.event === 'update'){
                openUpdateSales(data);
            }
        });


        //打开修改当前物品数量的弹出层
        function openUpdateSales(data){
            selectItem = data ;
            update_id = layer.open({
                type: 1,
                title: "修改["+data.goodsname+"]数量",
                content: $("#updateSalesDiv"),
                success: function(index){
                    $("#updateDataFrm")[0].reset();
                }
            })
        }

        //物品数量弹出层监听
        form.on("submit(doSubmit4)",function(data){

            var nums = $("#goods_num").val();
            $.get(api+"goods/getGoodsByGoodId",{goodsid:selectItem.id},function(res){
                if (res.data.number < nums){
                    layer.msg("当前库存仅"+res.data.number+"件，输入数值超出！");
                    return false;
                }else{
                    tabledata.forEach(element => {
                        if(selectItem.id == element.id){
                            element.number = nums;
                        }
                    });
                    //同步信息
                    tabledata = checkPrice_Num(memberState, tabledata);
                    layer.close(update_id);
                    return false;
                }
            })
        })


        //自定义校验方式
		form.verify({
			//退货校验
			withSomething: function(value, item){ //value：表单的值、item：表单的DOM对象
                var str;
                if (tabledata.length == 0 || tabledata.length < 0){
                    str = "未有商品扫描";
                }
                return str;
		  	},
              nonenil: function(value, item){
                  var str;
                  if(value <= 0){
                      str="修改的商品数量不得小于1";
                  }
                  return str;
              }
              
        })



    });
</script>

    <script src="../../../js/quagga.js" type="text/javascript"></script>
    <!-- <script src="../../../js/index.js" type="text/javascript"></script> -->
    <script src="../../../js/prism.js"></script>
    <script src="https://serratus.github.io/quaggaJS/javascripts/scale.fix.js"></script>

</body>
</html>